//
//  MNewFeatureVC.swift
//  iOSweibo
//
//  Created by lzm on 16/5/15.
//  Copyright © 2016年 lzm. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"
private let itemCount = 4

class MNewFeatureVC: UICollectionViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.collectionView?.registerClass(MNewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        prepaerFlowLayout()
    }
    
    //collectionView 流布局
    private func prepaerFlowLayout(){
        layout.itemSize = view.bounds.size
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        
        collectionView?.bounces = false
        collectionView?.showsHorizontalScrollIndicator = false
        collectionView?.pagingEnabled = true
        
    }
    
    let layout = UICollectionViewFlowLayout()
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //构造函数
    init(){
        super.init(collectionViewLayout: layout)
    }
    
    //MARK: collectionview数据源方法
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return itemCount
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! MNewFeatureCell
        cell.imgIndex = indexPath.item
        
        
        return cell
    }
    //scrollView代理方法
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        let index = Int(scrollView.contentOffset.x / scrollView.bounds.width)
        
        if index == itemCount - 1{
            let cell = collectionView?.cellForItemAtIndexPath(NSIndexPath(forItem: index, inSection: 0)) as! MNewFeatureCell
            cell.startBtnAnimation()
        }
    }
}
